<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/2
 * Time: 10:06
 */

namespace app\admin\controller\hotel;


use app\admin\controller\BaseController;
use app\admin\model\House;
use app\admin\model\Position;
use think\Controller;
use think\Session;

class Index extends BaseController
{
    public function page(){
        $house=new House();
        $user=(new Session())->get("user");
        if($user["scope"]==0){
            $items=$house
                ->where([
                    "is_del"        =>0,
                    "store_id"      =>$user["id"]
                ])
                ->paginate(15);
        }else{
            $items=$house
                ->where([
                    "is_del"        =>0,
                ])
                ->paginate(15);
        }

        $page=$items->render();
        $this->assign("items",$items);
        $this->assign("page",$page);
        return $this->fetch("index");
    }

    public function edit(){
        $house=new House();
        $position=new Position();
        if($this->request->isGet()){
            if($this->request->has("id")){//编辑
                $id=$this->request->get("id");
                $item=$house->find([
                    "id"    =>$id
                ]);
                //dump($item);die();
                $cities=$position->getCityOrArea($item->province);
                $areas=$position->getCityOrArea($item->city);
                $this->assign("cities",$cities);
                $this->assign("areas",$areas);
                $this->assign("item",$item);
            }else{//添加

            }
            $provinces=$position->getProvince();
            $this->assign("provinces",$provinces);
            $this->assign("attrArr",$house->attriArr);
            $this->assign("matchArr",$house->matchArr);
            $this->assign("structArr",$house->structArr);
            $this->assign("levelArr",$house->levelArr);
            $this->assign("typeArr",$house->typeArr);
            $this->assign("funStyleArr",$house->funStyleArr);
            return $this->fetch();
        }else{
            $data=$this->request->param();
            //dump($data);die();
            if(!empty($data["id"])){//更新
                $house::update($data);
                return json([
                    "code"      =>200,
                    "msg"       =>"update success"
                ]);
            }else{//插入
                unset($data["id"]);
                $house->save($data);
                return json([
                    "code"      =>200,
                    "msg"       =>"insert success"
                ]);
            }
        }

    }

    public function getCity(){
        $postion=new Position();
        $code=$this->request->param("code");
        $data=$postion->getCityOrArea($code);
        if(!$data){
            return json([
                "code"      =>3000,
                "errmsg"    =>"failed"
            ]);
        }else{
            return json([
                "code"      =>200,
                "msg"       =>"success",
                "data"      =>$data
            ]);
        }
    }

    public function del(){
        $id=$this->request->param("id");
        $house=new House();
        $res=$house::update([
            "id"        =>$id,
            "is_del"    =>1
        ]);
        if($res){
            return json([
                "code"      =>200,
                "msg"       =>"success"
            ]);
        }
    }

    public function pullOrPush(){
        $data=$this->request->param();
        $house=new House();
        $res=$house::update($data);
        if($res){
            return json([
                "code"      =>200,
                "msg"       =>"success"
            ]);
        }
    }
}